import { Component, OnInit, ViewChild } from "@angular/core";
import "datatables";
import { FormBuilder } from "@angular/forms";
import { ApprInfo } from "./appr-info.class";
import { PublishManService } from "./publishing-management.service";
import { DataTablesComponent, DataTablesComponentConfig } from "app/theme/components/dataTables";
import { SearchForm } from './search-form.class';
import { PublishManInfo } from "./publishing-management-appr.class";

@Component({
  selector: 'publishing-management',
  templateUrl: 'publishing-management.component.html',
  styleUrls: ['publishing-management.component.scss'],
  providers: [PublishManService]
})
export class PublishManComponent implements OnInit {
  @ViewChild(DataTablesComponent) dataTable: DataTablesComponent;

  private applyUrl: string = '/water/boards/saveNotice';
  private applyAllUrl: string = '/water/boards/showAllScore';
  private applyTableUrl: string = '/water/boards/showScoreList';
  private apprInfo: ApprInfo = new ApprInfo();
  private _selectdAppr: PublishManInfo;
  private searchForm: SearchForm = new SearchForm();

  private disablePublishBtn = true;

  private selectedRowData: any[];
  private _errorMsg: string;
  private errorShow = false;

  private dataTablesConfig: DataTablesComponentConfig = new DataTablesComponentConfig();

  constructor(private _fb: FormBuilder, private _service: PublishManService) {
  }

  search() {
    this.dataTable.draw();
  }

  ngOnInit() {
    this.dataTablesConfig.ajaxUrl = this.applyTableUrl;
    this.dataTablesConfig.columnsDef = [
      {'name': '证书类型', 'id': 'type'},
      {'name': '姓名', 'id': 'name'},
      {'name': '身份证', 'id': 'idcard'},
      {'name': '公司名', 'id': 'companyname'},
      {'name': '录入时间', 'id': 'submittime'},
      {'name': '成绩', 'id': 'score'}
    ];
    this.initDatepicker();
  }

  private initDatepicker() {
    let rightNow = new Date();
    jQuery('#starttime').datepicker('setDate', rightNow);
    let endate = new Date(rightNow.setDate(rightNow.getDate() + 7));
    jQuery('#endtime').datepicker('setDate', endate);
  }

  tableRowSelect(rowData: any[]) {
    this.disableAllButtons();
    if (rowData.length > 0) {
      this.disablePublishBtn = rowData.length === 0;
      this.selectedRowData = rowData;
    }
  }

  handleError(e:any){
    if(e.type == 'error'){
      this.errorShow = true;
    }
  }

  private disableAllButtons() {
    this.disablePublishBtn = true;
  }

  onHideModal(): void {
    jQuery('.modal-backdrop').remove();
  }

  private subSelected() {
    let ids = [];
    for (let row of this.selectedRowData) {
      ids.push(row.ids);
    }
    this.apprInfo.boards_certi_ids = ids;
    let starttime: any = document.getElementById('starttime');
    this.apprInfo.boards_startdate = starttime.value;
    let endtime: any = document.getElementById('endtime');
    this.apprInfo.boards_enddate = endtime.value;
    this._service.submitSelected(this.applyUrl, this.apprInfo).then(result => {
      if (result.status === 'success') {
        this.dataTable.draw(false);
      } else {
        this._errorMsg = result.data;
      }
    }, error => {
      this._errorMsg = error;
    });
  }

  private subAll() {
    let info: any = {};
    let starttime: any = document.getElementById('starttime');
    info.startdate = starttime.value;
    let endtime: any = document.getElementById('endtime');
    info.enddate = endtime.value;
    this._service.submitAll(this.applyAllUrl, Object.assign(info, this.searchForm)).then(result => {
      if (result.status === 'success') {
        this.dataTable.draw(false);
      } else {
        this._errorMsg = result.data;
      }
    }, error => {
      this._errorMsg = error;
    });
  }
}
